package com.kyzh.core.pager.home

import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.WindowManager
import com.gushenge.core.base.activity.BaseVmDbActivity
import com.gushenge.core.base.viewmodel.BaseViewModel
import com.gushenge.core.toast
import com.kdp.starbarcode.core.BarCodeScanConfig
import com.kdp.starbarcode.core.BarCodeType
import com.kdp.starbarcode.inter.OnBarCodeScanResultListener
import com.kyzh.core.R
import com.kyzh.core.databinding.ActScanBinding
import com.kyzh.core.pager.me.user.ScanLoginActivity
import java.util.*


class ScanActivity : BaseVmDbActivity<BaseViewModel,ActScanBinding>(R.layout.act_scan) {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, ScanActivity::class.java)
            context.startActivity(starter)
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        val windowManager: WindowManager = windowManager
        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val screenWidth: Int = dm.widthPixels
        val screenHeight: Int = dm.heightPixels
        val scanWidth = screenWidth / 6 * 4
        val scanHeight = screenHeight / 3
        val left = (screenWidth - scanWidth) / 2
        val top = (screenHeight - scanHeight) / 2
        val right = scanWidth + left
        val bottom = scanHeight + top
        mDatabind.scanView.setBorder(intArrayOf(left, top, right, bottom))
        val rect = Rect(left, top, right, bottom)
        val barCodeScanConfig = BarCodeScanConfig.Builder()
            .setROI(rect) //识别区域
            .setBarCodeType(BarCodeType.QR_CODE) //仅识别二维码，可提升识别速度
            .setSupportAutoZoom(true) //当二维码图片较小时自动放大镜头(仅支持QR_CODE)
            .build()
        mDatabind.barcodepreview.barCodeScanConfig = barCodeScanConfig;
        mDatabind.barcodepreview.setOnBarCodeScanResultListener(object :
            OnBarCodeScanResultListener{
            override fun onSuccess(result: String?) {
                if (result != null) {
                    if (result.contains("LoginToken")) {
                        ScanLoginActivity.start(this@ScanActivity, result)
                        finish()
                    } else {
                        kotlin.runCatching {
                            if (result.startsWith("http://") || result.startsWith("https://")) {
                                val url = Uri.parse(result)
                                val intent = Intent().apply {
                                    action = "android.intent.action.VIEW"
                                    data = url
                                }
                                startActivity(intent)
                                finish()

                            } else {
                                mDatabind.barcodepreview.startRecognize();
                                "二维码无效".toast()
                            }
                        }.onFailure {
                            mDatabind.barcodepreview.startRecognize();
                            "二维码无效".toast()
                        }
                    }
                    Log.e("TAG", "onActivityResult: "+result )
                }
            }
            override fun onFailure() {
//                mDatabind.barcodepreview.startRecognize();
                Log.e("TAG", "onFailure: ", )
            }
        })
    }

    override fun onStart() {
        super.onStart()
        mDatabind.barcodepreview.openCamera()
    }

    override fun onResume() {
        super.onResume()
                mDatabind.barcodepreview.startRecognize();

    }

    override fun onDestroy() {
        super.onDestroy()
        mDatabind.barcodepreview.stopRecognize();
        mDatabind.barcodepreview.closeCamera();
    }
}